<?php /*0*/

/**
  *	This file contains a class
  *
  *	@author Vytenis Urbonavicius <vytenis.urbonavicius@myhost.lt>
  *	@package CMS
  *	@version 0.0
  */

if ( !defined( 'SECURITY' ) ) {
	die( 'Direct access to this script is not allowed!' );
}

/**
  *	This class is responsible for complex operations with files
  *
  *	@author Vytenis Urbonavicius <vytenis.urbonavicius@myhost.lt>
  *	@version 0.0
  */
class ComplexFile {
	
	/**
	  *	This method is able to gather a whole tree of files
	  *
	  * <br /><br />
	  *	Note that parameters are located in an associative array
	  *
	  *	@author Vytenis Urbonavicius <vytenis.urbonavicius@myhost.lt>
	  *	@access public
	  *	@version 0.0
	  *
	  * @static
	  *
	  * @param array $arguments - this is an associative array <br /><br />
	  * It contains: <br />
	  * 	- address - string file address <br />
	  * 	- external_address - bool flag which tells function that file tree is outside the project <br />
	  *
	  *	@return array/false array of files ( with full addresses ) or false on error
	  */
	public static function getFileTree( $arguments ) {
		
		$address = Arguments::getArgument( array( 'title' => 'address', 'argument_array' => $arguments ) );
		
		if ( !$address || !is_string( $address ) ) {
			return false;
		}
		
		$external_address = Arguments::getArgument( array( 'title' => 'external_address', 'argument_array' => $arguments ) );
		
		if ( !$external_address ) {
			$address = SimpleFile::getFullFileAddress( array( 'address' => $address ) );
		}
		
		if ( false === $files = SimpleFile::getFiles( array( 'address' => $address, 'external_address' => $external_address ) ) ) {
			return false;
		}
		
		$result = array();
		
		if ( !empty( $files ) ) {
			foreach( $files as $file ) {
				if ( ( false !== strpos( $file, '.' ) ) || ( false ===@ opendir( $address . '/' . $file ) ) ) {
					$result[] = $address . '/' . $file;
				} else {
					$inner_files = ComplexFile::getFileTree( array( 'address' => $address . '/' . $file, 'external_address' => $external_address ) );
					if ( !empty( $inner_files ) ) {
						foreach( $inner_files as $inner_file ) {
							$result[] = $inner_file;
						}
					}
				}
			}
		}
		
		return $result;
		
	}
	
}

?>